"""
信号处理模块
用于处理数据库模型变更时的自动操作
"""
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from .models import MedicalArticle

@receiver(post_save, sender=MedicalArticle)
def sync_article_to_es_on_save(sender, instance, **kwargs):
    """
    当文章保存时，同步到Elasticsearch
    """
    # 导入放在函数内部避免循环导入
    from .es_utils.es_sync import ElasticsearchSyncManager
    ElasticsearchSyncManager.sync_article_to_es(instance)

@receiver(post_delete, sender=MedicalArticle)
def remove_article_from_es_on_delete(sender, instance, **kwargs):
    """
    当文章删除时，从Elasticsearch中删除
    """
    # 导入放在函数内部避免循环导入
    from .es_utils.es_sync import ElasticsearchSyncManager
    ElasticsearchSyncManager.sync_article_to_es(instance, delete=True)